package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.dao.*;
import com.example.demo.pojo.*;
import com.example.demo.service.PoetryService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private PoetryService poetryService;
    @Autowired
    private PoetryDao poetryDao;
    @Autowired
    private DynastyDao dynastyDao;
    @Autowired
    private AuthorDao  authorDao;
    @Autowired
    private YiwenDao yiwenDao;
    @Autowired
    private GushiDao gushiDao;
    @Autowired
    private ZhushiDao zhushiDao;
    @Autowired
    private ShangxiDao shangxiDao;


    @RequestMapping("toindex")
    public String toindex(){
        return "index";
    }
    @RequestMapping("toyiwen")
    public String toyiwen(){
        return "yiwen";
    }
    @RequestMapping("togushi")
    public String togushi(){
        return "gushi";
    }
    @RequestMapping("tozhushi")
    public String tozhushi(){
        return "zhushi";
    }
    @RequestMapping("toshangxi")
    public String toshangxi(){
        return "shangxi";
    }
    @RequestMapping("tologin")
    public String tologin(){
        return "login";
    }
    @RequestMapping("login")
    public String login(User user){
        User u= userService.login(user);
        if (u!=null){
            return "redirect:listall";
        }else {
            return "login";
        }
    }
    @RequestMapping("toabout")
    public String toabout(Model model, Integer id){
        System.out.println(id);
        Poetry list = poetryDao.selectById(id);
        System.out.println(list);
        QueryWrapper<Dynasty> map2 = new QueryWrapper<>();
        Page<Dynasty> poetryPage2 = new Page<Dynasty>(1,12);
        Page<Dynasty> poetrylist2 = dynastyDao.selectPage(poetryPage2,map2);
        QueryWrapper<Author> map3 = new QueryWrapper<>();
        Page<Author> poetryPage3 = new Page<Author>(1,20);
        Page<Author> poetrylist3 = authorDao.selectPage(poetryPage3,map3);
        model.addAttribute("authorlst",poetrylist3);
        model.addAttribute("dynastylst",poetrylist2);
        model.addAttribute("list",list);
        return "about";
    }
    @RequestMapping("listall")
    public String listall(Model model,Integer pageing){
        if(pageing == null){
            pageing =1;
        }
        QueryWrapper<Poetry> map = new QueryWrapper<>();
        Page<Poetry> poetryPage = new Page<Poetry>(pageing,2);
        Page<Poetry> poetrylist = poetryDao.selectPage(poetryPage,map);
        QueryWrapper<Dynasty> map2 = new QueryWrapper<>();
        Page<Dynasty> poetryPage2 = new Page<Dynasty>(1,12);
        Page<Dynasty> poetrylist2 = dynastyDao.selectPage(poetryPage2,map2);
        QueryWrapper<Author> map3 = new QueryWrapper<>();
        Page<Author> poetryPage3 = new Page<Author>(1,20);
        Page<Author> poetrylist3 = authorDao.selectPage(poetryPage3,map3);
        model.addAttribute("authorlst",poetrylist3);
        model.addAttribute("dynastylst",poetrylist2);
        model.addAttribute("list",poetrylist);
        return "index";
    }
    @RequestMapping("addyiwen")
    public String  addyiwen(Yiwen yiwen){
        yiwenDao.insert(yiwen);
        return  "redirect:toabout";
    }
    @RequestMapping("addGushi")
    public String  addGushi(Gushi gushi){
        gushiDao.insert(gushi);
        return  "redirect:toabout";
    }
    @RequestMapping("addZhushi")
    public String  addZhushi(Zhushi zhushi){
        zhushiDao.insert(zhushi);
        return  "redirect:toabout";
    }
    @RequestMapping("addShangxi")
    public String  addShangxi(Shangxi shangxi){
        shangxiDao.insert(shangxi);
        return  "redirect:toabout";
    }
}
